<?php
/*=====================================================
 Copyright (C) ETERNAL<iqstar@ms24.hinet.net>
 URL : http://www.et99.net

请仔细阅读以下许可协议。当您使用本软体，您将自动成为本协议的一方并受到本协议的约束。

软体和文档受到台湾及中国大陆著作权法及国际条约的保护。您不得：
a)营利、出租或者出借软体或文档的任何部分。
b)反向工程、分解、反编译或者企图察看软体的源代码作为商业用途。
c)修改或者改变软体，或者与其他程式结合。

许可人保留软体及文档的所有权利和权益。您不能通过本许可协定获得软体的任何所有权和知识产权。
===================================================== */
require_once dirname(__FILE__) . '/../libs/pdo_database.php';
require_once dirname(__FILE__) . '/../utils/item.php';
require_once dirname(__FILE__) . '/../controller/playerItemController.php';

class wog_fight_event
{
    var $temp_fight_string = "";
    var $mercenary_topr = 1;
    var $mercenary_get = "";

    /**
     * 查询掉落物，重置掉落概率
     */
    function getMonsterDf($md_id, $d_type_start = 0, $d_type_end = 4)
    {
        $pdoClass = new PDODatabase();
        $db = $pdoClass->connect();
        $temp_md_id = gettype($md_id) == 'string' ? explode(',', $md_id) : $md_id;
        $df = $db->select('wog_df', '*', ['d_id' => $temp_md_id]);
        $result = [];
        foreach ($df as $k => $value) {
            $result[$value['d_id']] = $value;
        }
        return $result;
    }

//########## get_item begin   ##########
    function get_item($user_id, $md_id, $m_topr, $p_st, $p_bag)
    {
        global $DB_site, $a_id, $wog_array, $wog_item_tool;
        $pdoClass = new PDODatabase();
        $db = $pdoClass->connect();
        // 重置掉落
        $all_df = $this->getMonsterDf($md_id, $m_topr, 0, 4);

        // 怪物最终掉落计算
        $temp_get_item = calcGetItemTopr($all_df, $md_id, $m_topr, $this->mercenary_topr, $p_st, 'string');
        echo json_encode($temp_get_item);

        if (empty($temp_get_item) || count($temp_get_item) < 1) {
            return false;
        }
        $playerItemController = new playerItemController($db);
        // 新背包入库
        foreach ($temp_get_item as $value) {
            $playerItemController->saveItem($user_id, $value['d_id'],null, 1);
        }
        //$DB_site->query_first("set autocommit=0");
        //$DB_site->query_first("BEGIN");
        $sql = "select a_id,d_body_id,d_foot_id,d_hand_id,d_head_id,d_item_id,d_stone_id,d_plus_id from wog_item where p_id=" . $user_id . " for update";
        // 查询背包
        $p_item = $DB_site->query_first($sql);
//        $sql = "select d_id,d_name,d_fie,d_vip,d_dbst from wog_df where d_id in (" . $temp_get_item . ") ";
//        $item = $DB_site->query($sql);
        $temp_sql = "";
        $temp_pack = array();
        $temp_pack2 = array();
        // 所有物品
        $result_items = array();
        // parent.get_item后面的背包参数
        $bag_param = '';
        foreach ($temp_get_item as $items) {
            // 非VIP遇到掉落VIP物品，则跳过
            if ($items['d_vip'] != $p_st) {
                break;
                continue;
            }
            // 物品的品质
            $quality_map = $wog_array['eq_quality'];
//            品质详情[name=>'',color=>'']
            $item_quality = $quality_map[$items['d_dbst']];

            // 武器的名称
            $a_id = type_name($items['d_type']);
            $p_item[$a_id] = trim($p_item[$a_id]);
            if (!empty($p_item[$a_id])) {
                $temp_pack[$a_id] = explode(",", $p_item[$a_id]);
            }
            if ($items['d_type'] > 4) {
                $bag = $wog_array["item_limit"] + $p_bag;
                $temp_pack2[$a_id] = $temp_pack[$a_id];
                $temp_pack[$a_id] = $wog_item_tool->item_in($temp_pack[$a_id], $items['d_id'], 1);
                echo ",'" . $items["d_name"] . ",";
                if (count($temp_pack[$a_id]) <= $bag) {
                    $d_name_html = "<strong style=color:{$item_quality['color']}>{$items["d_name"]}</strong>";
                    array_push($result_items, $d_name_html);
//                    $this->temp_fight_string .= ",\"parent.get_item('{$d_name_html}',1)\"";
                    $bag_param = ',1';
                    $this->mercenary_get .= "，" . $items['d_name'];
                } else {
                    $temp_pack[$a_id] = $temp_pack2[$a_id];
                    $d_name_html = "<strong style=color:{$item_quality['color']}>{$items['d_name']}</strong>";
                    array_push($result_items, $d_name_html);
                    $bag_param = ",0,{$bag}";

//                    $this->temp_fight_string .= ",\"parent.get_item('{$d_name_html}',0,$bag)\"";
                }
            } else {
                $bag = $wog_array["item_limit"];
                if (count($temp_pack[$a_id]) + 1 <= $bag) {
                    $temp_pack[$a_id] = $wog_item_tool->item_in($temp_pack[$a_id], $items['d_id']);
                    $d_name_html = "<strong style=color:{$item_quality['color']}>{$items['d_name']}</strong>";
                    array_push($result_items, $d_name_html);
                    $bag_param = ",1";

//                    $this->temp_fight_string .= ",\"parent.get_item('{$d_name_html}',1)\"";
                    $this->mercenary_get .= "，" . $items['d_name'];
                } else {
                    $d_name_html = "<strong style=color:{$item_quality['color']}>{$items['d_name']}</strong>";
                    $bag_param = ",0,{$bag}";
//                    $this->temp_fight_string .= ",\"parent.get_item('{$d_name_html}',0,$bag)\"";
                    array_push($result_items, $d_name_html);
                }
            }
            $p_item[$a_id] = implode(',', $temp_pack[$a_id]);
        }
        // 转为字符串
        $result_items = implode($result_items, '、');
        $this->temp_fight_string .= ",\"parent.get_item('{$result_items}'{$bag_param})\"";
        unset($p_item, $a_id);
        $DB_site->free_result($items);
        foreach ($temp_pack as $key => $value) {
            $temp_sql .= "," . $key . "='" . implode(',', $value) . "'";
        }
        if (!empty($temp_sql)) {
            $temp_sql = substr($temp_sql, 1);
            $DB_site->query("update wog_item set " . $temp_sql . " where p_id=" . $user_id);
        }
        //$DB_site->query_first("COMMIT");
        //$DB_site->query_first("set autocommit=1");
        unset($temp_pack);
    }

    function exchange_up(&$p, &$m, $type)
    {
        global $DB_site, $lang;
        $sql = "select ex_id,ex_name,ex_money from wog_exchange_main ORDER BY RAND() LIMIT 1";
        $exchange_check = $DB_site->query_first($sql);
        $time = time();
        if ($m[m_meet] >= 20) {
            $temp_ex1 = rand(1, 5);
        } else {
            $temp_ex1 = rand(0, 2);
        }
        if ($type == 1) {
            $temp_ex4 = rand(100, 101);
        } else {
            $temp_ex4 = rand(1, 6);
        }
        $temp_ex2 = floatval($temp_ex1 . "." . rand(1, 99));
        $exchange_check[ex_money] = $exchange_check[ex_money] + (($exchange_check[ex_money] * $temp_ex2) / 100);
        $sql = "update wog_exchange_main set ex_money=" . $exchange_check[ex_money] . ",ex_change=1,ex_chg_num=" . $temp_ex2 . ",ex_change_time=" . $time . " where ex_id=" . $exchange_check[ex_id];
        $DB_site->query($sql);
        $temp_str = sprintf($lang['wog_fight_change_mg' . $temp_ex4], $p[p_name], $m[m_name], $exchange_check[ex_name], $exchange_check[ex_name], "$temp_ex2%");
        $temp_chat = $temp_str;
        $sql = "insert wog_exchange_book(ex_id,eb_body,eb_time)values($exchange_check[ex_id],'$temp_str',$time)";
        $DB_site->query($sql);
        $temp_str = $DB_site->insert_id();
        $sql = "delete from wog_exchange_book where eb_id <= " . ($temp_str - 8);
        $DB_site->query($sql);
        /*
                require_once("./class/wog_act_message.php");
                $wog_act_class = new wog_act_message;
                $wog_act_class->system_chat("<font color=\"red\">".$temp_chat."</font>");
                unset($wog_act_class);
        */
        unset($temp_ex1, $temp_ex2, $temp_ex4);
    }

    function exchange_down(&$p, &$m, $type)
    {
        global $DB_site, $lang;
        $sql = "select ex_id,ex_name,ex_money from wog_exchange_main ORDER BY RAND() LIMIT 1";
        $exchange_check = $DB_site->query_first($sql);
        $time = time();
        if ($m[m_meet] >= 10) {
            $temp_ex1 = rand(1, 5);
        } else {
            $temp_ex1 = rand(0, 2);
        }
        $temp_ex2 = floatval($temp_ex1 . "." . rand(1, 99));
        $exchange_check[ex_money] = $exchange_check[ex_money] - (($exchange_check[ex_money] * $temp_ex2) / 100);
        if ($type == 1) {
            $temp_ex4 = rand(50, 52);
            $temp_str = sprintf($lang['wog_fight_change_mg' . $temp_ex4], $p[p_name], $m[m_name], $exchange_check[ex_name], $exchange_check[ex_name], "$temp_ex2%");
        } else {
            $temp_ex4 = rand(53, 61);
            $temp_str = sprintf($lang['wog_fight_change_mg' . $temp_ex4], $m[m_name], $p[p_name], $exchange_check[ex_name], $exchange_check[ex_name], "$temp_ex2%");
        }
        $sql = "";
        $sql = "update wog_exchange_main set ex_money=" . $exchange_check[ex_money] . ",ex_change=2,ex_chg_num=" . $temp_ex2 . ",ex_change_time=" . $time . " where ex_id=" . $exchange_check[ex_id];
        $DB_site->query($sql);
        $temp_chat = $temp_str;
        $sql = "insert wog_exchange_book(ex_id,eb_body,eb_time)values($exchange_check[ex_id],'$temp_str',$time)";
        $DB_site->query($sql);
        $temp_str = $DB_site->insert_id();
        $sql = "delete from wog_exchange_book where eb_id <= " . ($temp_str - 8);
        $DB_site->query($sql);
        /*
                require_once("./class/wog_act_message.php");
                $wog_act_class = new wog_act_message;
                $wog_act_class->system_chat("<font color=\"red\">".$temp_chat."</font>");
                unset($wog_act_class);
        */
        unset($temp_ex1, $temp_ex2, $temp_ex4);
    }

//########## event_start  ##########
    function event_start($user_id, $p_attempts)
    {
        global $DB_site, $wog_array, $lang;
        if ($p_attempts > $wog_array["attempts"]) {
            $DB_site->query("update wog_player set p_lock=1,p_lock_time=" . time() . " where p_id=" . $user_id);
            $DB_site->query_first("COMMIT");
            cookie_clean();
            alertWindowMsg(sprintf($lang['wog_fight_event_lock'], $wog_array["unfreeze"]));
        } else {
//			$get_key=$this->gen_key();
            $DB_site->query("update wog_player set p_attempts=p_attempts+1 where p_id=" . $user_id);
            $DB_site->query_first("COMMIT");
        }
        showscript("parent.event();");
    }

//########## event_creat  ##########
    function event_creat($user_id)
    {
        global $DB_site, $wog_array;
//		$get_key=$this->gen_key();
        $DB_site->query("update wog_player set p_attempts=1  where p_id=" . $user_id);
        $DB_site->query_first("COMMIT");
        showscript("parent.event();");
    }

    //Gen Key.
    function gen_key()
    {
        global $wog_array;
//		$key = "";
        //u can add your own image below here.
        $confirm_chars = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9');

        list($usec, $sec) = explode(' ', microtime());
        mt_srand($sec * $usec);

        $max_chars = count($confirm_chars) - 1;
        $key = '';
        for ($i = 0; $i < $wog_array["keylen"]; $i++) {
            $key .= $confirm_chars[mt_rand(0, $max_chars)];
        }
        /*
                $count = count($chars) - 1;
                srand((double)microtime()*1000000);
                for($i = 0; $i < $wog_array["keylen"]; $i++)
                {
                    $key .= $chars[rand(0, $count)];
                }
        */
        return ($key);
    }


//########## pet_stats  ##########

    function pet_stats($d_id)
    {
        switch ($d_id) {
            case 258:
                $lv = 1;
                break;
            case 1837:
                $lv = 8;
                break;
            case 2236:
                $lv = 13;
                break;
        }
        $pet[pe_at] = rand(1, 10) * $lv;
        $pet[pe_mt] = rand(1, 10) * $lv;
        $pet[pe_def] = rand(1, 10) * $lv;
        $pet[pe_type] = rand(1, 4);
        return $pet;
    }
    /*
    //####################   Gen Image with GD   ####################
        //####################   GD JPEG   ####################
        function img_gd_jpeg($name,$words){
            $words_size=strlen($words);
            for($i=0;$i<$words_size;$i++)
            {
                $this_number[$i]=substr($words,$i,1);
                $this_size_temp=getimagesize("anti-bot/validation_".$this_number[$i].".jpg");
                $this_width[$i]=$this_size_temp[0];
                $this_height[$i]=$this_size_temp[1];
                $image_width+=$this_size_temp[0];
            }
            $image_height=max($this_height);

            $base=@imagecreate($image_width,$image_height);
            if(!$base){header("location:anti-bot/error2.jpg");exit;}

            for($i=0;$i<$words_size;$i++)
            {
                $image=imagecreatefromjpeg("anti-bot/validation_".$this_number[$i].".jpg") or die("false 2");
                imagecopyresized($base,$image,0+$sum_width,0,0,0,$this_width[$i],$this_height[$i],$this_width[$i],$this_height[$i]) or die("false 3");
                $sum_width+=$this_width[$i];
                imagedestroy($image);
            }
            imagejpeg($base,"${name}.jpg");
            imagedestroy($base);
        }

    //####################   GD GIF   ####################
        function img_gd_gif($name,$words){
            $words_size=strlen($words);

            for($i=0;$i<$words_size;$i++)
            {
                $this_number[$i]=substr($words,$i,1);
                $this_size_temp=getimagesize("anti-bot/validation_".$this_number[$i].".gif");
                $this_width[$i]=$this_size_temp[0];
                $this_height[$i]=$this_size_temp[1];
                $image_width+=$this_size_temp[0];
            }
            $image_height=max($this_height);

            $base=@imagecreate($image_width,$image_height);
            if(!$base){header("location:anti-bot/error2.gif");exit;}

            for($i=0;$i<$words_size;$i++)
            {
                $image=imagecreatefromgif("anti-bot/validation_".$this_number[$i].".gif") or die("false 2");
                imagecopyresized($base,$image,0+$sum_width,0,0,0,$this_width[$i],$this_height[$i],$this_width[$i],$this_height[$i]) or die("false 3");
                $sum_width+=$this_width[$i];
                imagedestroy($image);
            }
            imagegif($base,"${name}.gif");
            imagedestroy($base);
        }
    //####################   Gen Image without GD   ####################
        function img_no_gd($words)
        {
            global $antibot;
            $words_size=strlen($words);
            for($i = 0; $i < $words_size; $i++)
            {
               $validation_images .= "<img src=http://www.233.idv.tw/wog/" . 'anti-bot/validation_' . $words{$i} .".". $wog_array["file_type"] . '>';
            }
            return $validation_images;
        }
    */
}

?>